import math
from dataclasses import dataclass

from ._typing import Number


@dataclass
class Point:
    x: float
    y: float


def clip(x: Number, bottom: Number, top: Number) -> Number:
    """限定数值范围"""
    if x < bottom:
        x = bottom
    if x > top:
        x = top
    return x


def compute_distance(*args) -> float:
    """
    * 传入两个点o1/o2：计算两点间距离
    * 传入三个点o1/o2/o3，计算o3到o1o2直线的距离
    """
    if len(args) == 2:
        o1, o2 = args
        d = math.sqrt((o1.x - o2.x) ** 2 + (o1.y - o2.y) ** 2)
    else:
        o1, o2, o3 = args
        a = o2.y - o1.y
        b = o1.x - o2.x
        c = o2.x * o1.y - o1.x * o2.y
        d = (a * o3.x + b * o3.y + c) / math.sqrt((a**2 + b**2 + 1e-3))
    return d


def atan(o1, o2) -> float:
    """
    计算o1/o2的相对角度，变换到(0, 2pi)
    """
    theta = math.atan2(o2.y - o1.y, o2.x - o1.x)
    if theta < 0:
        theta += 2 * math.pi
    return theta
